import 'package:dio/dio.dart';

/// created by hcl
/// on 2024/2/21
/// RequestLog
/// desc
class RequestLog implements Interceptor {
  ///是否打印日志到控制台
  static bool enablePrintLog = true;

  ///错误数据采集
  @override
  Future onError(DioError err, ErrorInterceptorHandler handler) async {
    print('ssss dio onError ${err.message}');
    if (err.response != null) saveResponse(err.response!);
    return handler.next(err);
  }

  ///请求体数据采集
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    print('ssss dio onRequest ${ options.uri.toString()}');
    print('ssss dio onRequest ${ options.method}');
    print('ssss dio onRequest ${ options.headers.toString()}');
    print('ssss dio onRequest ${ options.queryParameters.toString()}');
    print('ssss dio onRequest ${ options.data.toString()}');
    return handler.next(options);
  }

  ///响应体数据采集
  @override
  Future onResponse(
      Response response, ResponseInterceptorHandler handler) async {
    saveResponse(response);
    return handler.next(response);
  }

  void saveResponse(Response response) {
    print('ssss dio saveResponse ${ response.statusCode.toString()}');
    print('ssss dio saveResponse ${ response.headers.toString()}');
    print('ssss dio saveResponse ${ response.data.toString()}');
  }
}
